package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.util.Iterator;

import javax.swing.border.LineBorder;

import MessagePacks.GetAllPack;
import MessagePacks.MessagePack;
import javax.swing.JTextField;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class EstimatorMenuScreen extends MasterGUI {

	private static final long serialVersionUID = 1L;

	private MainGUI mainGui;
	private JButton btnTimeEstimation;
	private JButton btnInsertReportEstimation;
	private JButton btnTimeExtensionRequest;
	private JPanel panel_1;
	private JPanel panel;
	private TimeEstimation estimatortime;
	private ShowRequest request;
	private EstimationReportSend estimatorreport;
	private GetAllPack getEstID;
	private int flag=0;
	private TimeExtensionRequest timeExtenRqst;
	private JLabel lblProcessId;
	private JTextField textField;
	private JButton btnReadRequest;
	
	
	public EstimatorMenuScreen( MainGUI mainGui) {
		super(mainGui);
		this.mainGui=mainGui;
		
		getContentPane().setForeground(Color.BLACK);
		
		panel = new JPanel();
		panel.setBounds(0, 0, 884, 40);
		getContentPane().add(panel);
		
		JLabel lblEstimatorMenu = new JLabel("Estimator Menu");
		lblEstimatorMenu.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblEstimatorMenu);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setForeground(Color.BLACK);
		panel_1.setBounds(0, 40, 176, 201);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		{
			lblProcessId = new JLabel("Process ID : ");
			lblProcessId.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblProcessId.setBounds(218, 51, 96, 25);
			getContentPane().add(lblProcessId);
		}
		{
			textField = new JTextField();
			textField.setFont(new Font("Tahoma", Font.PLAIN, 12));
			textField.setBounds(337, 55, 86, 20);
			getContentPane().add(textField);
			textField.setColumns(10);
			textField.setText(String.valueOf(mainGui.process.getprocessID()));
			textField.setEditable(false);
		}
		
		
		setBtnTimeExtensionRequest();
		setBtnInsertReportEstimation();
		setBtnTimeEstimation();
		SetBtnReadRequest();
		
		


	}

	public JButton getBtnTimeEstimation() {
		return btnTimeEstimation;
	}


	public void setBtnTimeEstimation() {
		btnTimeEstimation = new JButton("Time Estimation");
		if(mainGui.process.getCurrentPhase()==1 && mainGui.process.getResponsableOK()==0){
			btnTimeEstimation.setEnabled(true);
			btnInsertReportEstimation.setEnabled(false);
			btnTimeExtensionRequest.setEnabled(false);
		}
		else{btnTimeEstimation.setEnabled(false);
		}
		btnTimeEstimation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estimatortime= new TimeEstimation(mainGui);
				estimatortime.setVisible(true);
				close();
			}
		});
		btnTimeEstimation.setBounds(10, 49, 156, 23);
		panel_1.add(btnTimeEstimation);
				
			}
	public JButton getBtnReadRequest() {
		return btnReadRequest;
	}
	public void SetBtnReadRequest(){
		btnReadRequest = new JButton("Read Request");
		btnReadRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			request= new ShowRequest(mainGui);
			request.setVisible(true);
			close();
		
			}
		});
		btnReadRequest.setBounds(10, 15, 156, 23);
		panel_1.add(btnReadRequest);
	}


	public JButton getBtnInsertReportEstimation() {
		return btnInsertReportEstimation;
	}


	public void setBtnInsertReportEstimation() {
		btnInsertReportEstimation = new JButton("Insert Report Estimation");
		getAllReports();
		checkIfReportExist();
		if(flag==1){
			btnInsertReportEstimation.setEnabled(false);
		}
		btnInsertReportEstimation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estimatorreport= new EstimationReportSend(mainGui);
				estimatorreport.setVisible(true);
				close();
				
			}
		});
		btnInsertReportEstimation.setBounds(10, 83, 156, 23);
		panel_1.add(btnInsertReportEstimation);
	}


	private void checkIfReportExist() {
			
		Iterator<String> iter = getEstID.nArray.iterator();
		while (iter.hasNext()){
			int procId = Integer.parseInt(iter.next());
			if(procId==mainGui.process.getprocessID()){
				this.flag=1;
			}
			
			
		}
		
	}

	public JButton getBtnTimeExtensionRequest() {
		return btnTimeExtensionRequest;
	}


	public void setBtnTimeExtensionRequest() {
		btnTimeExtensionRequest = new JButton("Time Extension Request");
		btnTimeExtensionRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				timeExtenRqst = new TimeExtensionRequest(mainGui);
				timeExtenRqst.setVisible(true);
				close();
				
			}
		});
		btnTimeExtensionRequest.setBounds(10, 117, 156, 23);
		panel_1.add(btnTimeExtensionRequest);
	}
	
	public void getAllReports(){
		 getEstID = new GetAllPack(MessagePack.getAllReports);
		 mainGui.client.handleMessageFromClientUI(getEstID);
		 getEstID= (GetAllPack) mainGui.client.getMessage();
	}

}


